package com.ihighest.app.infox.activity;

import java.util.LinkedList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ViewPagerAdapter;
import com.ihighest.app.infox.myview.TouchImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

/**
 * 图片大图查看
 * 
 * 
 * @author Vity Created at 2015-11-5 下午11:01:28
 * @version 1.0
 */
public class ImageViewActivity extends BaseActivity {

	List<String> imageUrls;

	ViewPager viewPager;
	ViewPagerAdapter adapter;

	TouchImageView imageView;

	TextView tv_seqc, tv_exit;

	// 当前位置

	int count = 0;

	// ImageLoader imageLoader;

	// DisplayImageOptions defaultOptions;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.infox_activity_image_view);
		imageUrls = this.getIntent().getStringArrayListExtra("images");
		if (imageUrls == null || imageUrls.isEmpty()) {
			if (!TextUtils.isEmpty(this.getIntent().getStringExtra("image"))) {
				imageUrls = new LinkedList<String>();
				imageUrls.add(this.getIntent().getStringExtra("image"));
			}
		}

		// imageLoader = ImageLoader.getInstance();
		// defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
		// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
		// .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
		initView();
	}

	void initView() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		imageView = (TouchImageView) findViewById(R.id.imageview);

		tv_seqc = (TextView) findViewById(R.id.tv_seqc);
		tv_exit = (TextView) findViewById(R.id.tv_exit);
		tv_exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		count = imageUrls.size();
		if (imageUrls.size() > 1) {
			tv_seqc.setVisibility(View.VISIBLE);
			tv_seqc.setText("1 / " + count);
			viewPager.setVisibility(View.VISIBLE);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			LinkedList<View> views = new LinkedList<>();
			for (int i = 0; i < imageUrls.size(); i++) {
				TouchImageView iv = new TouchImageView(this);
				iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
				iv.setLayoutParams(params);
				AppKit.SetNormalImg(imageUrls.get(i), iv);
				// ImageLoader.getInstance().displayImage(imageUrls.get(i), iv,
				// defaultOptions);
				views.add(iv);     
			}      
			viewPager = (ViewPager) findViewById(R.id.viewpager);
			// 初始化Adapter
			adapter = new ViewPagerAdapter(views);
			viewPager.setAdapter(adapter);
			viewPager.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
			// 绑定回调
			viewPager.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(int position) {
					tv_seqc.setText((position + 1) + " / " + count);
				}

				@Override
				public void onPageScrolled(int arg0, float arg1, int arg2) {

				}

				@Override
				public void onPageScrollStateChanged(int arg0) {

				}
			});

		} else if (!imageUrls.isEmpty()) {
			imageView.setVisibility(View.VISIBLE);
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					finish();
				}
			});
			AppKit.SetNormalImg(imageUrls.get(0), imageView);
			// imageLoader.displayImage(imageUrls.get(0), imageView,
			// defaultOptions);
		}
	}

}
